package com.stn.zjx.givingtreasure.activity.userwallet;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.OrientationHelper;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.activity.userbankcard.UserBankCardManagerActivity;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.domain.GetUserMoneyDomain;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;
import com.stn.zjx.givingtreasure.net.BaseApi;
import com.stn.zjx.givingtreasure.net.impl.UserMoneyIntegralImpl;
import com.stn.zjx.givingtreasure.service.UserService;
import com.stn.zjx.givingtreasure.table.User;
import com.stn.zjx.givingtreasure.utils.StatusBarUtil;
import com.stn.zjx.givingtreasure.widget.RunningTextView;
import com.vondear.rxtools.RxActivityTool;


import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserWalletActivity extends BaseActivity {
    @BindView(R.id.back_bt)
    RelativeLayout backBt;
    @BindView(R.id.action_title)
    TextView actionTitle;
    @BindView(R.id.detailed_bt)
    TextView detailedBt;
    @BindView(R.id.money_tv)
    RunningTextView moneyTv;
    @BindView(R.id.go_recharge_bt)
    RelativeLayout goRechargeBt;
    @BindView(R.id.go_withdraw_bt)
    RelativeLayout goWithdrawBt;
    @BindView(R.id.go_ggt_bt)
    RelativeLayout goGgtBt;
    @BindView(R.id.go_bank_card_bt)
    RelativeLayout goBankCardBt;

    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }

    @Override
    public void initComponent() {
        DecimalFormat df=new DecimalFormat("0.00");
        moneyTv.setText(df.format(0));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initStatusBarColor(R.color.transparentBlank_5);
        StatusBarUtil.setImmersiveStatusBar(this, false);
        setContentView(R.layout.activity_user_wallet);
        ButterKnife.bind(this);
        initComponent();
    }

    @OnClick({R.id.back_bt,
            R.id.go_recharge_bt, R.id.go_withdraw_bt, R.id.go_ggt_bt, R.id.go_bank_card_bt,
    R.id.go_transfer_bt,R.id.detailed_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.detailed_bt:
                 RxActivityTool.skipActivity(this, UserTransferRecordListActivity.class);
                break;
            case R.id.back_bt:
                finish();
                break;

            case R.id.go_recharge_bt:

                RxActivityTool.skipActivity(this, UserRechargeActivity.class);
                break;
            case R.id.go_withdraw_bt:
                RxActivityTool.skipActivity(this, UserWithDrawActivity.class);
                break;
            case R.id.go_ggt_bt:
                break;
            case R.id.go_bank_card_bt:
                RxActivityTool.skipActivity(this, UserBankCardManagerActivity.class);
                break;
            case R.id.go_transfer_bt:
                RxActivityTool.skipActivity(this, UserTransferActivity.class);
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        getMoney();
    }

    public void getMoney(){
       User onlineUser=UserService.getInstance(this).getOnlineLoginUser();
         if(onlineUser==null){
           return;
         }
        UserMoneyIntegralImpl.get_money(onlineUser.getToken(), onlineUser.getToken() + "_get_money", new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                 if(response.isSuccessful()&&!isFinishing()){
                     initMoney(response.body());
                 }
            }

            @Override
            public void onCacheSuccess(Response<String> response) {
                super.onCacheSuccess(response);
                if(response.isSuccessful()&&!isFinishing()){
                    initMoney(response.body());
                }
            }
        });
    }
    public void initMoney(String body){
        GetUserMoneyDomain domain=new Gson().fromJson(body,GetUserMoneyDomain.class);
              switch (domain.getStatus()){
                  case BaseApi.REQUEST_SUCCESS_CODE:
                      try {
                          if(domain.getData().getUserMoney()!=Double.parseDouble(moneyTv.getText().toString().trim())){
                              DecimalFormat df=new DecimalFormat("0.00");
                              moneyTv.playNumber(domain.getData().getUserMoney());
                          }
                      }catch (Exception e){
                           e.printStackTrace();
                      }

                      break;
              }
    }
}
